package com.titaniume.exer;

import org.junit.Test;

import java.io.*;

/**
 * @program: JavaSenior
 * @description:
 * @author: renfei.huang
 * @create: 2021-10-17 21:14
 */
public class PicTest {



    //图片加密
    @Test
    public void test1() throws IOException {

        //FileInputStream fileInputStream = new FileInputStream(new File("彼岸图网.jpg"));
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //简写 省略new File()
            fileInputStream = new FileInputStream("彼岸图网.jpg");
            fileOutputStream = new FileOutputStream("彼岸图网secret.jpg");



            byte []  buffer = new byte[1024];
            int len;

            while ((len = fileInputStream.read(buffer))!=-1) {
                //字节数组进行修改
                //错误的
//            for (byte b :buffer){
//               b =  (byte)(b ^ 5);
//            }

                //正确的
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }

                fileOutputStream.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {

            if(fileInputStream!=null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        }


     //图片解密
    @Test
    public void test2() throws IOException {

        //FileInputStream fileInputStream = new FileInputStream(new File("彼岸图网.jpg"));
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //简写 省略new File()
            fileInputStream = new FileInputStream("彼岸图网secret.jpg");
            fileOutputStream = new FileOutputStream("彼岸图网4.jpg");

            byte []  buffer = new byte[1024];
            int len;

            while ((len = fileInputStream.read(buffer))!=-1) {
                //字节数组进行修改
                //错误的
//            for (byte b :buffer){
//               b =  (byte)(b ^ 5);
//            }

                //正确的
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }

                fileOutputStream.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {

            if(fileInputStream!=null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    }


